package com.notary.tx_live_plugin.platform;

import android.content.Context;

import com.notary.tx_live_plugin.FlavorConfigImpl;
import com.notary.tx_live_plugin.util.TxConstant;

import java.util.HashMap;
import java.util.Map;

import io.flutter.Log;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;

/**
 * 控制当前plugin插件中PlatformView的创建工厂Factory
 */
public class TxLivePlatformFactory extends PlatformViewFactory {

    private final BinaryMessenger binaryMessenger;
    
    public TxLivePlatformFactory(BinaryMessenger binaryMessenger) {
        super(StandardMessageCodec.INSTANCE);
        this.binaryMessenger = binaryMessenger;

    }

    @Override
    public PlatformView create(Context context, int viewId, Object args) {
        HashMap<String,Object> params = null;
        if(args instanceof Map){
            params = (HashMap<String, Object>) args;
        }
        String type = params != null ? (String)params.get(TxConstant.ViewType.view) : null;
        if(TxConstant.ViewType.PLAY.equals(type)){
            return new TxLivePlayPlatformView(binaryMessenger,context,viewId,params);
        }else if(TxConstant.ViewType.CHAT.equals(type)){
            PlatformView view = FlavorConfigImpl.getInstance().getChatView(binaryMessenger,context,viewId,params);
            if(view != null) return view;
        } else if(TxConstant.ViewType.PUSH.equals(type)){
            return new TxLivePushPlatformView(binaryMessenger,context,viewId,params);
        }
        Log.d("tx_live_plugin", "TxLivePlatformFactory unSupport PlatformView for viewId "
                + viewId + " args "
                + args.toString() + " " + (params == null ? "" : params.toString()));
        return null;
    }

}
